// proxySubShopDevice.js
const app = getApp()
const util = require("../../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    search: {
      searchAddShow: false,
      BtnTxt: '添加设备',
      searchTip: '输入设备号搜索',
      searchValue: '',
    },
    id:'',
    list:[],
    pagenum:1,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.shopDevice('defalut');
  },
  searchChange: function (event) {
    var detail = 'search.searchValue'
    this.setData({
      [detail]: event.detail
    })
  },
  // 搜索
  onSearch: function () {
    this.shopDevice('search');
  },
  // 获取列表
  shopDevice: function (type){
    var that = this;
    if (type == 'search') {
      that.setData({ list: [] })
    }
    var data = { id: that.data.id,page: that.data.pagenum, input: that.data.search.searchValue };
    app.post("agent/shopDevice", data).then((res) => {
      if (res.code == 200) {

        var list = that.data.list;
        var dataArr = res.list;
        list = list.concat(dataArr)
        that.setData({
          list: list
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);//错误提示信息
    })
    wx.stopPullDownRefresh()

  },
  // 删除
  deleteTap:function(e){
    var that = this;
    var sn = e.currentTarget.dataset.sn;
    var data = {sn:sn}
    wx.showModal({
      title: '提示',
      content: '真的要删除吗？',
      success: function (res) {
        if (res.confirm) {

          wx.showLoading({
            title: '加载中...',
          })
          app.post('agent/deleteDevice', data).then((res) => {
            wx.hideLoading();
            if (res.code == 200) {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000
              })
              that.setData({
                list: [],
                pagenum: 1
              })
              that.shopDevice('defalut')
            } else {
              wx.showToast({
                title: "删除失败",
                icon: 'loading',
                duration: 2000
              })
            }
          }).catch((errMsg) => {
            console.log(errMsg);
            wx.hideLoading();
            wx.showToast({
              title: errMsg,
              icon: 'loading',
              duration: 2000
            })
          })
          
        }
      }
    })
  },
  onPullDownRefresh: function () {
    // 请求最新数据
    this.setData({
      list: [],
      pagenum: 1
    });
    this.shopDevice('defalut');


  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var pagenum = that.data.pagenum + 1; //获取当前页数并+1
    that.setData({
      pagenum: pagenum, //更新当前页数
    })
    that.shopDevice();//重新调用请求获取下一页数据
  }

})